const query = {
  type: '1',
  page: '1',
  limit: '3'
}

const myCollect = async () => {

  const { data: { data } } = await axios.get('/user/userDataList', { params: query })
  const { list: { count, totalPages, pageSize, currentPage } } = data

  const htmlStr = data.list.data.map(ele => {
    const { catename, id, title, author, keywords, lovenum, pic, tags, click, description, ishot, istop, create_date } = ele
    let picSrc
    if (pic) {
      picSrc = `http://124.223.14.236:8060/${pic}`
    }
    else {
      picSrc = `./static/images/default.png`
    }
    const date = new Date(create_date)
    const year = date.getFullYear()
    let month = date.getMonth() + 1
    let day = date.getDate()
    month = month < 10 ? '0' + month : month
    day = day < 10 ? '0' + day : day
    return `
            <article class="excerpt excerpt-one">
              <header>
                <a class="cat label label-important" href="">热门<i class="label-arrow"></i></a>
                <h2>
                  <a target="_blank" href="show.html" title="${title}">${title}</a>
                </h2>
              </header>
              <p class="text-muted time">
                <a href="">${author}</a> 发布于 ${year}-${month}-${day}
              </p>
              <p class="focus">
                <a target="_blank" href="http://blog.weknow.cn/474.shtml" class="thumbnail">
                  <span class="item"><span class="thumb-span"><img src="${picSrc}"
                        class="thumb" /></span></span></a>
              </p>
              <p class="note">${description}
              </p>
              <p class="text-muted views">
                <span class="post-views">阅读(${click||0})</span>
                <span class="post-comments">评论(0)</span>
                <a href="javascript:;" class="post-like">
                  <i class="fa fa-thumbs-up"></i>赞 (<span>${lovenum||0}</span>)
                </a>
              
                <button class="btn btn-xs btn-danger" data-id="${id}"><i class="fa fa-trash"></i>&nbsp;取消收藏</button>
              </p>
            </article>
    
    `
  }).join('')
  console.log(htmlStr)
  $('.list-box').html(htmlStr || '暂无文章数据。。。')
  if (!count) {
    $('#page-box').hide()
  }
  renderPage(count)
}
myCollect()
$('.list-box').on('click', 'button', async function () {
  const id = $(this).attr('data-id')
  const res = await axios.get('/user/userDataHandle', {
    params: {
      type: 1,
      article_id: id,
      action: 'del',
    }
  })
  layer.msg('取消收藏成功', {
    icon: 1,
    offset: '20px'
  })
  myCollect()
})


const laypage = layui.laypage
function renderPage(count) {
  laypage.render({
    elem: 'page-box'
    , count: count
    , theme: '#FF5722'
    , limit: '3'
    , curr: query.page
    , last: '尾页'
    , layout: ['page', 'next', 'count']
    , jump: function (obj, first) {
      //obj包含了当前分页的所有参数，比如：
      // console.log(obj.curr); //得到当前页，以便向服务端请求对应页的数据。
      // console.log(obj.limit); //得到每页显示的条数
      query.page = obj.curr
      query.limit = obj.limit
      //首次不执行
      if (!first) {
        myCollect()
      }
    }
  })
}
